package app

import (
	"go.manyji.com/lib/config"
	"go.manyji.com/lib/encoding/json"
	"go.manyji.com/lib/mysql"
	"net/http"
)

var (
	fDBAddr = config.String("db.mysql.addr", "127.0.0.1:3306", "mysql addr")
	fDBName = config.String("db.mysql.name", "", "mysql db name")
	fDBUser = config.String("db.mysql.user", "", "mysql db user")
	fDBPwd  = config.String("db.mysql.pwd", "", "mysql db password")
)

var (
	db *mysql.DB
)

func InitDb() (*mysql.DB, error) {

	var err error
	db, err = mysql.Open(*fDBAddr, *fDBUser, *fDBPwd, *fDBName)
	return db, err
}

func HandleStats(w http.ResponseWriter, r *http.Request) {

	s := db.Stats()
	b, _ := json.MarshalIndent(s, "", " ")
	_, _ = w.Write(b)
}
